package com.haitaoit.chengquan.module.home.activity.fabu;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.home.model.TitleModel;
import com.haitaoit.chengquan.module.login.LoginActivity;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.pickerview.OptionsPopupWindow;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;

public class SendServiceActivity extends BaseActivity {
    @ViewInject(R.id.service_type_tv)
    TextView service_type_tv;
    @ViewInject(R.id.send_view1)
    LinearLayout send_view1;
    @ViewInject(R.id.send_view2)
    LinearLayout send_view2;
    @ViewInject(R.id.send_view3)
    LinearLayout send_view3;
    @ViewInject(R.id.send_view4)
    LinearLayout send_view4;
    @ViewInject(R.id.send_view5)
    LinearLayout send_view5;
    @ViewInject(R.id.send_view6)
    LinearLayout send_view6;


    private int typeId = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_send_service);
        setTitle_V("发布服务");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);

        changeView();
    }

    @OnClick({R.id.service_type_tv})
    public void onClick(View v) {// 收起键盘
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        switch (v.getId()) {
            case R.id.service_type_tv:
                SelectType();

                if (PreferenceUtils.getPrefString("user_id", "").equals("")) {
                    Intent intent = new Intent(this, LoginActivity.class);
                    intent.putExtra("tag", "0");
                    startActivity(intent);
                } else if (!PreferenceUtils.getPrefString("account_type", "").equals("1")) {
                } else {
                    ToastUtils.showToast("服务商才可以发布招聘！");
                }
                break;
        }
        super.onClick(v);
    }

    public void changeView() {
        if (typeId == 1) {
            send_view1.setVisibility(View.VISIBLE);
            send_view2.setVisibility(View.GONE);
            send_view3.setVisibility(View.GONE);
            send_view4.setVisibility(View.GONE);
            send_view5.setVisibility(View.GONE);
            send_view6.setVisibility(View.GONE);
        } else if (typeId == 2) {
            send_view1.setVisibility(View.GONE);
            send_view2.setVisibility(View.VISIBLE);
            send_view3.setVisibility(View.GONE);
            send_view4.setVisibility(View.GONE);
            send_view5.setVisibility(View.GONE);
            send_view6.setVisibility(View.GONE);
        } else if (typeId == 3) {
            send_view1.setVisibility(View.GONE);
            send_view2.setVisibility(View.GONE);
            send_view3.setVisibility(View.VISIBLE);
            send_view4.setVisibility(View.GONE);
            send_view5.setVisibility(View.GONE);
            send_view6.setVisibility(View.GONE);
        } else if (typeId == 4) {
            send_view1.setVisibility(View.GONE);
            send_view2.setVisibility(View.GONE);
            send_view3.setVisibility(View.GONE);
            send_view4.setVisibility(View.VISIBLE);
            send_view5.setVisibility(View.GONE);
            send_view6.setVisibility(View.GONE);
        } else if (typeId == 5) {
            send_view1.setVisibility(View.GONE);
            send_view2.setVisibility(View.GONE);
            send_view3.setVisibility(View.GONE);
            send_view4.setVisibility(View.GONE);
            send_view5.setVisibility(View.VISIBLE);
            send_view6.setVisibility(View.GONE);
        } else if (typeId == 6) {
            send_view1.setVisibility(View.GONE);
            send_view2.setVisibility(View.GONE);
            send_view3.setVisibility(View.GONE);
            send_view4.setVisibility(View.GONE);
            send_view5.setVisibility(View.GONE);
            send_view6.setVisibility(View.VISIBLE);
        }
    }

    private void SelectType() {
        final ArrayList<TitleModel> listAll = new ArrayList<TitleModel>();
        listAll.add(new TitleModel("1", "加工订单"));
        listAll.add(new TitleModel("2", "机床耗材"));
        listAll.add(new TitleModel("3", "机床配件"));
        listAll.add(new TitleModel("4", "机床组立"));
        listAll.add(new TitleModel("5", "二手设备买卖"));
        listAll.add(new TitleModel("6", "二手设备改造"));
        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < listAll.size(); i++) {
            list.add(listAll.get(i).getName());
        }

        OptionsPopupWindow popSex = new OptionsPopupWindow(this, "");
        popSex.setPicker(list);
        popSex.setSelectOptions(0);
        popSex.setOnoptionsSelectListener(new OptionsPopupWindow.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                service_type_tv.setText(listAll.get(options1).getName());
                typeId = Integer.parseInt(listAll.get(options1).getId());
                changeView();
            }
        });
        popSex.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // popupwindow消失的时候恢复成原来的透明度
                backgroundAlpha(1f);
            }
        });
        popSex.showAtLocation(service_type_tv, Gravity.BOTTOM, 0, 0);
        backgroundAlpha(0.6f);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0-1.0
        getWindow().setAttributes(lp);
    }
}
